<?php
/**
 * Created by V.K Chow GameCC Studio DevTeam.
 * File: IndexController.class.php
 * User: xiaoyem
 * Date: 14-1-11
 * Time: 下午1:40
 */
namespace Home\Controller;

use Think\Hook;

/**
 * 首页控制器
 * Class IndexController
 * @package Home\Controller
 */
class IndexController extends HomeBaseController
{

    /**
     * 构造函数
     */
    function __construct()
    {

    	define('APP_DEBUG', false);
        parent::__construct();
        $this->getLink();
    }

    /**
     * 显示首页
     */
    public function index()
    {

        $this->getNews();
        $this->display('index');

    }

    /**
     * 显示首页为空时
     * @param $method
     * @param $args
     */
    public function _empty($method, $args)
    {
        Hook::listen('home_index_empty');

    }

    public function aboutus(){
    	$this->display('aboutus');
    }
    
    public function news(){
        $this->cat_init();
        $this->display('news');
    }
    public function cat_init(){
        $cats =D('cats');
        $cat_name = $cats->where("cat_father = 0")->field("cat_id,cat_name")->select();
        $this->assign('cats_names',$cat_name);
        $cat_id=$_GET['ci'];
        $this->typeNews($cat_id);


    }
    public function success(){
    	$this->display('success');
    }
    
    public function statute(){
    	$this->display('statute');
    }
    
    public function contact(){
    	$this->display('contact');
    }

    private function getNews(){
        $news=D("posts");
        $info=$news->order('post_date desc')->limit(0,7)->select();
        $post_cat = D('post_cat');
        for($i=0;$i<count($info);++$i){
            $cat_id = $post_cat->where("post_id={$info[$i]['post_id']}")->field('cat_id')->select();
            $info[$i]['cat_id'] = $cat_id[0]['cat_id'];
        }
        $this->assign('post_info',$info);
    }

    private function typeNews($cat_id=1){
        $cat=D("post_cat");
        $post_id=$cat->order('post_id desc')->where("cat_id=$cat_id")->limit(0,7)->field("post_id")->select();
        $news=D("posts");
        $cats = D('cats');
        $cat_name = $cats->where("cat_id=$cat_id")->field('cat_name')->select();
        $this->assign('cat_name',$cat_name[0]);

        $cat_info = [];
        foreach($post_id as $v){
            $post_info=$news->where("post_id=$v[post_id]")->order('post_date desc')->select();
            $post_info[0]['ci'] = $cat_id;
            array_push($cat_info,$post_info[0]);
        }
        $this->assign('cat_info',$cat_info);

    }
    private function getLink(){
        $link = D("links");
        $link_info = $link->order('link_sort desc')->limit(0,20)->select();
        $this->assign('link_info',$link_info);
    }
}